package org.example.day;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/08/26 20:16
 */
public class Solution690 {

    Map<Integer, Integer> importanceMap = new HashMap<>();
    Map<Integer, List<Integer>> subordinatesMap = new HashMap<>();

    public int getImportance(List<Employee> employees, int id) {

        for (Employee employee : employees) {
            importanceMap.putIfAbsent(employee.id, employee.importance);
            subordinatesMap.putIfAbsent(employee.id, new ArrayList<>());
            subordinatesMap.get(employee.id).addAll(employee.subordinates);
        }

        return dfsImportance(id, subordinatesMap.get(id));
    }

    private int dfsImportance(int id, List<Integer> subordinates) {
        if(!importanceMap.containsKey(id)){
            return 0;
        }
        int sum = importanceMap.get(id);
        if(subordinates == null || subordinates.isEmpty()){
            return sum;
        }
        for (Integer subordinate : subordinates) {
            sum += dfsImportance(subordinate, subordinatesMap.get(subordinate));

        }
        return sum;
    }

    class Employee {
        public int id;
        public int importance;
        public List<Integer> subordinates;
    };

}
